package com.ws.zc.user.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ws.zc.api.user.UserService;
import com.ws.zc.mapper.user.entity.SysUser;
import com.ws.zc.mapper.user.entity.SysUserInfo;
import com.ws.zc.mapper.user.mapper.SysUserInfoMapper;
import com.ws.zc.mapper.user.mapper.SysUserMapper;
import com.ws.zc.mapper.user.param.RegisterUser;
import com.ws.zc.mapper.user.param.UpdateUser;
import com.ws.zc.mapper.user.param.UpdateUserInfo;
import com.ws.rain.base.exception.DaoException;
import com.ws.rain.base.exception.ParamException;
import com.ws.rain.base.utils.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


@RestController
@ContextConfiguration
public class UserServiceImpl implements UserService {
    private SysUserMapper userMapper;
    private SysUserInfoMapper userInfoMapper;
    private PasswordEncoder passwordEncoder;

    public PasswordEncoder passwordEncoder(){
        if(passwordEncoder==null){
            passwordEncoder = new BCryptPasswordEncoder();
        }
        return passwordEncoder;
    }

    @Autowired
    public UserServiceImpl(SysUserMapper userMapper,SysUserInfoMapper userInfoMapper){
           this.userMapper = userMapper;
           this.userInfoMapper = userInfoMapper;
    }

    @Transactional(rollbackFor = DaoException.class)
    @Override
    public SysUser register(RegisterUser user) {
        SysUserInfo userInfo = new SysUserInfo(null,null,user.getHeadIcon(),user.getRealName(),user.getGender(),user.getPhone(),user.getSelfNum(),new Date());
        int uiRow =userInfoMapper.insert(userInfo);
        if(uiRow==0){
            throw new DaoException("注册信息插入数据失败");
        }
        EntityWrapper<SysUserInfo> ew = new EntityWrapper<>();
        ew.eq("phone",user.getPhone());
        List<SysUserInfo> userInfoList = userInfoMapper.selectList(ew);
        if(userInfoList.size()==0||userInfoList.size()>1){
            throw new DaoException("查询数据失败");
        }
        String password = passwordEncoder().encode(user.getPassword());
        SysUser sysUser = new SysUser(null,null,null,password,user.getUsername(),user.getRoleId(),userInfoList.get(0).getId());
        int uRow = userMapper.insert(sysUser);
        if(uRow==0){
            throw new DaoException("用户信息插入数据失败");
        }
        EntityWrapper<SysUser> uew = new EntityWrapper<>();
        uew.eq("username",user.getUsername());
        List<SysUser> userList = userMapper.selectList(uew);
        if(userList.size()==0||userList.size()>1){
            throw new DaoException("用户信息查询失败");
        }
        return userList.get(0);
    }

    @Override
    public SysUserInfo updateUserInfo(UpdateUserInfo user) {
        SysUser tempUser = userMapper.selectById(user.getId());
        if(tempUser==null){
            throw new DaoException("修改的用户不存在");
        }
        SysUserInfo sysUserInfo = userInfoMapper.selectById(tempUser.getUserInfoId());
        if(StringUtil.isNotBlank(user.getGender())){
            sysUserInfo.setGender(user.getGender());
        }
        if(StringUtil.isNotBlank(user.getRealName())){
            sysUserInfo.setRealName(user.getRealName());
        }
        if(StringUtil.isNotBlank(user.getPhone())){
            sysUserInfo.setPhone(user.getPhone());
        }
        if(StringUtil.isNotBlank(user.getSelfNum())){
            sysUserInfo.setSelfNum(user.getSelfNum());
        }
        int row = userInfoMapper.updateById(sysUserInfo);
        if(row==0){
            throw new DaoException("用户修改失败");
        }
        return sysUserInfo;
    }

    @Override
    public String updateUser(UpdateUser user) {
        if(user.getId()==0&&StringUtil.isBlank(user.getPassword())||user.getId()==0&&user.getRoleId()==0){
            throw new ParamException("参数效验异常");
        }
        SysUser tempUser = userMapper.selectById(user.getId());
        if(tempUser==null){
            throw new DaoException("修改的用户不存在");
        }
        if(StringUtil.isNotBlank(user.getPassword())){
            String password = passwordEncoder().encode(user.getPassword());
            tempUser.setPassword(password);
        }
        if(user.getRoleId()!=0){
            tempUser.setRoleId(user.getRoleId());
        }
        userMapper.updateById(tempUser);
        return "修改成功";
    }

    @Override
    public SysUser loginSuccess(String username) {
        EntityWrapper<SysUser> ew = new EntityWrapper<>();
        ew.eq("username",username);
        SysUser sysUser = userMapper.selectList(ew).get(0);
        sysUser.setPassword("[PROTECT]");
        return sysUser;
    }

}
